package com.zebra.register;

import com.zebra.common.ZebraBaseMessage;
import com.zebra.remoting.connection.Connection;
import com.zebra.remoting.protocol.ZebraCommand;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;

import java.util.List;

public class RegisterSendMessageUtil {

    private static final InternalLogger logger = InternalLoggerFactory.getInstance(RegisterSendMessageUtil.class);
    public static void pushGw(int gatewaySid, int cmd, long userId, long tk, ZebraBaseMessage message){
        Connection connection = RegisterConfigInfoManager.getInstance().getGwConnection(gatewaySid);
        if (null == connection || !connection.isFine()){
            logger.error("pushGw gateway is null; gwId: {}", gatewaySid);
            return;
        }

       ZebraCommand command = ZebraCommand.builder()
               .channel(connection.getChannel())
               .cmd(cmd)
               .token(tk)
               .userId(userId)
               .build();
       command.sendMessage(message);
    }


    public static void pushGwAll(int cmd,  ZebraBaseMessage message){
        List<Connection> list = RegisterConfigInfoManager.getInstance().getGateways();
        if (null == list || list.isEmpty()){
            logger.error("PushGwAll gateway is null");
            return;
        }
        list.forEach(c -> {
            logger.info("PushGwAll cmd:{} gwId:{}", cmd, c.getServerId());
            ZebraCommand command = ZebraCommand.builder()
                    .channel(c.getChannel())
                    .cmd(cmd)
                    .build();
            command.sendMessage(message);
        });

    }


}
